//
//  DHSizeSelectViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/19.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSizeSelectViewController.h"
#import "DHCityListCell.h"

@interface DHSizeSelectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightLayout;
@property (strong, nonatomic) NSArray *sizesArray;



@end

@implementation DHSizeSelectViewController

- (instancetype)initWithSizesArray:(NSArray *)sizesArray
{
    self = [super init];
    if (self) {
        self.sizesArray = sizesArray;
    }
    return self;
}

- (NSArray *)sizesArray{
    if (!_sizesArray) {
        _sizesArray = [NSArray array];
    }
    return _sizesArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#4A4A4A"];
    
    self.tableViewHeightLayout.constant = 57 * self.sizesArray.count;
    
    [self.tableView registerClass:[DHCityListCell class] forCellReuseIdentifier:kDHCityListCell];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sizesArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHCityListCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCityListCell forIndexPath:indexPath];
    NSDictionary *size = [self.sizesArray objectAtIndex:indexPath.row];
    if ([[size objectForKey:@"inventory"] integerValue] - [[size objectForKey:@"locked"] integerValue] > 0) {
        cell.titleLabel.textColor = [UIColor whiteColor];
    }else{
        cell.titleLabel.textColor = RGB(100, 100, 100);
    }
    cell.titleLabel.text = [size objectForKey:@"size"];
    if (indexPath.row == 0) {
        cell.topLineView.hidden = NO;
    }else{
        cell.topLineView.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *size = [self.sizesArray objectAtIndex:indexPath.row];
    tableView.userInteractionEnabled = NO;
    if ([[size objectForKey:@"inventory"] integerValue] - [[size objectForKey:@"locked"] integerValue] > 0) {
        [self dismissViewControllerAnimated:YES completion:^{
            [[DHNetAPIClient shareDHNetAPIClient] request_cart_addWithParams:@{@"type":@"租赁",@"_id":[size objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
                tableView.userInteractionEnabled = YES;
                [[GlobalManager shareGlobalManager] refreshCartNumberAndBlock:^(id data, NSError *error) {
                    [NSObject showHudTipStr:@"成功添加到购物车"];
                }];
            }];
        }];
    }else{
        tableView.userInteractionEnabled = YES;
    }
}

@end
